package com.auxgroup.smarthome.inner.controller;

import com.auxgroup.bridge.business.inner.dto.AppPushTemplateDto;
import com.auxgroup.bridge.business.inner.vo.AppPushTemplateVo;
import com.auxgroup.smarthome.inner.service.AppPushTemplateService;
import com.auxgroup.smarthome.syscode.Syscode;
import com.auxgroup.smarthome.utils.PageContent;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by niuGuangzhe on 2017/8/7.
 */
@Api(description = "推送模板", tags = "推送模板相关")
@RestController
public class AppPushTemplateController {

    @Autowired
    private AppPushTemplateService appPushService;

    @ApiOperation(value = "推送模板-查询", notes = "推送模板-查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词(标题&内容)", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "pushTimeStart", value = "开始推送时间 非必填 格式严格按照‘yyyy-MM-dd HH:mm’", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "pushTimeEnd", value = "结束推送时间 非必填 格式严格按照‘yyyy-MM-dd HH:mm’", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "progress", value = "0:已保存 1：已发送 2：已删除 暂时保留，不传递参数默认返回（已发送&已保存）", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/push/templates", method = RequestMethod.GET)
    public ApiResponse<PageContent<AppPushTemplateVo>> getAppPushTemplate(@RequestParam(required = false) String keyword,
                                                                          @RequestParam(required = false) String pushTimeStart,
                                                                          @RequestParam(required = false) String pushTimeEnd,
                                                                          @RequestParam(required = false) Integer progress,
                                                                          @RequestParam(defaultValue = "0", required = false) Integer page,
                                                                          @RequestParam(defaultValue = "10", required = false) Integer size) {
        Date dateStart = null;
        Date dateEnd = null;
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            if(null != pushTimeStart){
                dateStart = format.parse(pushTimeStart);
            }
            if(null != pushTimeEnd){
                dateEnd = format.parse(pushTimeEnd);
            }
        } catch (ParseException e) {
            return new ApiResponse(Syscode.DTO_PARAMS_ERROR.getCode(), Syscode.DTO_PARAMS_ERROR.getMsg(), "");
        }
        return appPushService.findAppPushTemplate(keyword, dateStart, dateEnd, progress, page, size);
    }


    @ApiOperation(value = "推送模版-新增", notes = "推送模板-新增")
    @RequestMapping(value = "/push/template", method = RequestMethod.POST)
    public ApiResponse addAppPushTemplate(@RequestBody AppPushTemplateDto dto) {
        return appPushService.addAppPushTemplate(dto);
    }

    @ApiOperation(value = "推送模版-修改", notes = "推送模板-修改")
    @RequestMapping(value = "/push/template", method = RequestMethod.PUT)
    public ApiResponse updateAppPushTemplate(@RequestBody AppPushTemplateDto dto, String templateId) {
        return appPushService.updateAppPushTemplate(templateId, dto);
    }

    @ApiOperation(value = "推送模版-删除", notes = "推送模板-删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "主键", paramType = "path", dataType = "string", required = true)
    })
    @RequestMapping(value = "/push/template", method = RequestMethod.DELETE)
    public ApiResponse deleteAppPushTemplate(String templateId) {

        return appPushService.deleteAppPushTemplate(templateId);
    }

    @ApiOperation(value = "推送模版-发布", notes = "推送模板-发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "主键", paramType = "form", dataType = "string", required = true)
    })
    @RequestMapping(value = "/push/send", method = RequestMethod.POST)
    public ApiResponse pushToJiGuangPaas(String templateId) {
        return appPushService.pushToPaas(templateId);
    }



    @ApiOperation(value = "推送模版-发布new", notes = "推送模板-发布new")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateId", value = "主键", paramType = "form", dataType = "string", required = true)
    })
    @RequestMapping(value = "/push/sendnew", method = RequestMethod.POST)
    public ApiResponse pushToJiGuangPaasNew(String templateId) {
        return appPushService.pushToPaasNew(templateId);
    }

}